<?php
function user_check($user_id,$menu_id){
$query = "SELECT * FROM daftar_menu a INNER JOIN daftar_menu_akses b
	ON a.menu_id=b.menu_id WHERE b.user_id = {$user_id} 
	AND a.menu_id = {$menu_id}";
	$result = my_query($query);
	if(my_num_rows($result) == 0){
		return false;
	}
	return true;
}

function loadpage_userakses(){
	$header = array( 
		' '=>array('style'=>'border-bottom:2px solid;width:8%'),
		'Workgroup'=>array('style'=>'border-bottom:2px solid;width:23%'),  
		'Sub Group'=>array('style'=>'border-bottom:2px solid;width:23%'),  
		'Modules'=>array('style'=>'border-bottom:2px solid;width:23%'),  
		'Sub Module'=>array('style'=>'border-bottom:2px solid;width:23%'),  
	);
	$query = "SELECT * FROM   daftar_menu  "; 
	$result =my_query($query);
	$row = array();
	while( $ey = my_fetch_array($result) ){
		$fr = '<input type="checkbox" name="a['.$ey['menu_id'].']" value="1" '.( user_check($_GET['uid'],$ey['menu_id']) ? ' checked="checked" ': '').'/>';
	
		$row[] = array(
			'a'=>position_text_align( $fr,'center'),
			'b'=> $ey['menu1'],
			'c'=> $ey['menu2'],
			'd'=>  $ey['menu3'],
			'e'=>  $ey['VISIBILITY'] =='Y' ? $ey['label']  : '',
		);
	}
	$datas = table_rows($row); 
	$menuontop = array(
		'RESET' => array('onclick'=>'javascript:document.fuserakses.reset()'),
		'SIMPAN' => array('onclick'=>'javascript:document.fuserakses.submit()')
	);
	$printmenu = additional_menu_on_list( $menuontop ,'');
	return $printmenu.'<form method="POST" name="fuserakses">'.table_builder($header , $datas ,  5 , false ).'</form>';
}

function stor_akses(){
	my_query("DELETE FROM daftar_menu_akses WHERE user_id = {$_GET['uid']}");
	foreach($_POST['a'] as $k=>$iv){
		$query = "INSERT INTO daftar_menu_akses SET user_id ={$_GET['uid']},menu_id={$k}";
		my_query($query);
	}
}